<template>
	<view class="uni-numbox">
		<view
			class="uni-numbox__minus"
			:class="{ 'uni-numbox--disabled': disableSubtract || disabled }"
			@click="_calcValue('subtract')"
		>
			-
		</view>
		<input
			class="uni-numbox__value"
			type="number"
			:disabled="disabled"
			:value="inputValue"
			@blur="_onBlur"
		/>
		<view
			class="uni-numbox__plus"
			:class="{ 'uni-numbox--disabled': disableAdd || disabled }"
			@click="_calcValue('add')"
		>
			+
		</view>
	</view>
</template>
<script>
export default {
	props: {
		value: {
			type: Number,
			default: 1,
		},
		min: {
			type: Number,
			default: 0,
		},
		max: {
			type: Number,
			default: 100,
		},
		step: {
			type: Number,
			default: 1,
		},
		disabled: {
			type: Boolean,
			default: false,
		},
		// 新增属性
		cid: {
			type: String,
			default: '',
		},
		index: {
			type: Number,
			default: 0,
		},
		pIndex: {
			type: Number,
			default: 0,
		},
	},
	data() {
		return {
			inputValue: this.value,
		};
	},
	computed: {
		disableSubtract() {
			return this.inputValue <= this.min;
		},
		disableAdd() {
			return this.inputValue >= this.max;
		},
	},
	watch: {
		value(val) {
			this.inputValue = val;
		},
		inputValue(val) {
			// this.$emit('change', val);
			this.$emit('change', {
				value: val,
				cid: this.cid,
				index: this.index,
				pIndex: this.pIndex,
			});
		},
	},
	methods: {
		_calcValue(type) {
			if (this.disabled) {
				return;
			}
			const scale = this._getDecimalScale();
			let value = this.inputValue * scale;
			const step = this.step * scale;
			if (type === 'subtract') {
				value -= step;
			} else if (type === 'add') {
				value += step;
			}
			if (value < this.min || value > this.max) {
				return;
			}
			this.inputValue = value / scale;
		},
		_getDecimalScale() {
			let scale = 1;
			// 浮点型
			if (~~this.step !== this.step) {
				scale = Math.pow(10, (this.step + '').split('.')[1].length);
			}
			return scale;
		},
		_onBlur(event) {
			let { value } = event.detail;
			if (!value) {
				this.inputValue = 0;
				return;
			}
			value = +value;
			if (value > this.max) {
				value = this.max;
			} else if (value < this.min) {
				value = this.min;
			}
			this.inputValue = value;
		},
	},
};
</script>
<style>
.uni-numbox {
	display: inline-flex;
	flex-direction: row;
	justify-content: flex-start;
	height: 45rpx;
	position: relative;
	align-items: center;
}

.uni-numbox:after {
	content: '';
	position: absolute;
	transform-origin: center;
	box-sizing: border-box;
	pointer-events: none;
	top: -50%;
	left: -50%;
	right: -50%;
	bottom: -50%;
	border-radius: 12rpx;
	transform: scale(0.5);
}

.uni-numbox__minus,
.uni-numbox__plus {
	margin: 0;
	background-color: #1abc9c;
	width: 45rpx;
	font-size: 45rpx;
	height: 100%;
	line-height: 42rpx;
	text-align: center;
	color: #fff;
	position: relative;
}

.uni-numbox__minus {
	background-color: #ca5454;
}

.uni-numbox__value {
	position: relative;
	background-color: #fff;
	width: 80rpx;
	height: 100%;
	border-top: 1px solid #eee;
	border-bottom: 1px solid #eee;
	font-size: 28rpx;
	line-height: 45rpx;
	text-align: center;
	padding: 0;
	/* 解决app端输入框高度超出问题 */
	min-height: 1.2rem;
	box-sizing: border-box;
}

/* 解决app端输入框高度超出问题(未实现) */
/* .uni-numbox input{
        height: 100%;
        line-height: 70rpx;
    } */

.uni-numbox__value:after {
	content: '';
	position: absolute;
	transform-origin: center;
	box-sizing: border-box;
	pointer-events: none;
	top: -50%;
	left: -50%;
	right: -50%;
	bottom: -50%;
	border-style: solid;
	border-color: #c8c7cc;
	border-left-width: 1px;
	border-right-width: 1px;
	border-top-width: 0;
	border-bottom-width: 0;
	transform: scale(0.5);
}

.uni-numbox--disabled {
	background: #f5f5f5;
	color: silver;
}
</style>
